<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Jobs
 *
 * @author ZzNinjakyozZ
 */
class Jobs extends CI_Controller{
    
    public $UserInfo;
    
    public function __construct() {
        parent::__construct();
    }
    
    public function Index($PostID = 0){
        if($PostID == 0){
            $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
            $CI =& get_instance();
            $CI->load->model('Category_model','Category');
            $Array_Data['Category'] = $CI->Category->GetAllCategory();
            $this->load->view('user_group3/jobs/index',$Array_Data);
        }
        else{
            $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
            $CI =& get_instance();
            $CI->load->model('Posts_model','Posts');
            $CI->load->model('Category_model','Category');
            $CI->load->model('Users2_model','Users2');
            $CI->load->model('Post_Applies_model','PA');
            $CI->load->model('Users2_ContactInform_model','Us2Contact');
            $CI->load->model('Users2_PostReceiveApplicants_model','Us2Recv');
            $Array_Data['Post'] = $CI->Posts->Get(array('PostID' => $PostID));
            if(empty($Array_Data['Post'])){
                $this->load->view('user_group3/jobs/not_found_post',$Array_Data);
                return;
            }
            $Array_Data['Post'] = $Array_Data['Post'][0];
            //Author Information
            $Author = $CI->Users2->GetInformation(array('RelUserEmail' => $Array_Data['Post']['RelUserEmail']));
            $Array_Data['Post']['Author'] = $Author[0];
            //End of author information
            $Category = $CI->Category->Get(array('CategoryID' => $Array_Data['Post']['Field']));
            $Array_Data['Post'] = array_merge($Array_Data['Post'], $Category[0]);
            
            //Get More Information about Contact and Address
            $AddressID = explode('@@',$Array_Data['Post']['AddressWorkPlaces']);
            $AddressID = $AddressID[0];

            $Address = $CI->Us2Contact->Get(array('ContactID'   =>  $AddressID));
            $Array_Data['Post']['MoreAddress'] = $Address[0];
            
            $ContactID = explode('@@',$Array_Data['Post']['ContactContent']);
            $ContactID = $ContactID[0];
            
            $Contact = $CI->Us2Contact->Get(array('ContactID'   =>  $AddressID));
            $Array_Data['Post']['MoreContact'] = $Contact[0];
            $Array_Data['Post']['StatusCode'] = 0;
            //Is Applied?
            $Array_Data['Post']['PA'] = $CI->PA->Get(array(
                'RelPostID' => $Array_Data['Post']['PostID'],
                'RelUserEmailGroup3' => $Array_Data['UserInfo']['UserEmail']
                ));
            if($Array_Data['Post']['PA']){
                $Array_Data['Post']['PA'] = $Array_Data['Post']['PA'][0];
                $Array_Data['Post']['StatusCode'] = 1;
                //IsReceived
                $Recv   = $CI->Us2Recv->Get(array(
                    'RelPostApplyID'        =>  $Array_Data['Post']['PA']['PostApplyID'],
                ));
                if($Recv){
                    $Recv = $Recv[0];
                    $Array_Data['Post']['StatusCode'] = 2;
                    $Recv['Content'] = str_replace('#APPNAME#', $Array_Data['UserInfo']['UserFullname'], $Recv['Content']);
                    $Array_Data['Post']['Recv'] = $Recv;
                }
            }
            $this->load->view('user_group3/jobs/post_view', $Array_Data);
        }
    }
    
    
    public function get_job()
    {
        $UserInfo = $this->session->userdata('UserInfo');
        $CI =& get_instance();
        $CI->load->model('Posts_model','Posts');
        $CI->load->model('Category_model','Category');
        $CI->load->model('Favorite_Posts_model','Favorite');
        
        $Titles = $_GET['filter-title'];
        $Conditions = $_GET['filter-condition'];
        $Values = $_GET['filter-value'];
        $Where_Statements = array();
        
        $CategoryID = $_GET['child-category'];
        $ParCategoryID = $_GET['parent-category'];

        $Where_Statements[] = "PostStatus = 1";
        if($CategoryID)
            $Where_Statements[] = "Field = ".$CategoryID;
        if($CategoryID==0 && $ParCategoryID != 0){
            $ChildCats = $CI->Category->Get(array('ParentID' => $ParCategoryID));
            $Fields_Statement = array();
            foreach($ChildCats as $ChildCat){
                $Fields_Statement[] = "Field = ".$ChildCat['CategoryID'];
            }
            $Fields_Statement = '('.implode(' OR ', $Fields_Statement).')';
            $Where_Statements[] = $Fields_Statement;
        }
        
        /*-----Filter------*/ 
        for($i = 0; $i < count($Titles); $i++){
            if($Conditions[$i] == "LIKE"){
                if($Titles[$i] == 'Province' || $Titles[$i] == 'District' || $Titles[$i] == 'City'){
                    if($Titles[$i] == 'District')
                        $Values[$i] = $Titles[$i].' '.$Values[$i];
                    else
                        $Values[$i] = $Values[$i].' '.$Titles[$i];
                    $Titles[$i] = 'AddressWorkPlaces';
                }
                $Values[$i] = "'%".$Values[$i]."%'";           
            }
            $statement = $Titles[$i] . " " . $Conditions[$i] . " " . $Values[$i];
            $Where_Statements[$Titles[$i]][] = $statement;
        }
        $temp = array();
        foreach($Where_Statements as $Where){
            if(is_array($Where))
                $temp[] = '('.implode(' OR ',$Where).')';
            else
                $temp[] = $Where;
        }
        $Where_Statements = implode(' AND ', $temp);
        $Posts = $CI->Posts->Query($Where_Statements,'PostDate desc', 10000, 0);
        
        /* End of Filter */
        /*----- Page Caculate ----*/
        if(isset($_GET['Page']))
            $Current_Page = $_GET['Page'];
        else
            $Current_Page = 1;
        $offset = ($Current_Page-1) * 10;

        $Array_Data['TotalNum'] = count($Posts);
        //Caculate Limit
        $limit = $Array_Data['TotalNum'] - (($Current_Page-1)*10);
        $limit = ($limit>=10)?10:$limit;

         $Posts = $CI->Posts->Query($Where_Statements,'PostDate desc', $limit, $offset);
         $Array_Data['Current_Page'] = $Current_Page;
        /*---- End of Page Caculate ----*/
        /* Find favorite */
        $Temps = array();
        foreach($Posts as $Post){
            $Post['IsFavorite'] = $CI->Favorite->Check(array(
                'RelPostID'     => $Post['PostID'],
                'RelUserEmail'  => $UserInfo['UserEmail']
            ));  
            $Temps[] = $Post;
        }
        $Array_Data['Posts'] = $Temps;
        //End of find Favorite
        $Array_Data['UserInfo'] = $UserInfo;
        if($CategoryID){
            $Category = $CI->Category->Get(array('CategoryID' => $CategoryID));
            $Category = $Category[0];
        }elseif(!$CategoryID && $ParCategoryID){
            $Category = $CI->Category->Get(array('CategoryID' => $ParCategoryID));
            $Category = $Category[0];
        }
        if(isset($Category) )
            $Array_Data['Category'] = $Category;
        $this->load->view('user_group3/jobs/posts_container', $Array_Data);
    }
    
    public function apply($ID){
        error_reporting(0);
        @ini_set('display_errors', 0);
        $UserInfo = $this->session->userdata('UserInfo');
        if($UserInfo['UserType'] != 'work') exit;
        $CI =& get_instance();
        $CI->load->model('Post_Applies_model','PA');
        $data = array(
            'RelUserEmailGroup3'    =>  $UserInfo['UserEmail'],
            'RelPostID'             =>  $ID,
            'ApplyDate'             =>  date('Y-m-d H:i:s'),
            'IntroductionContent'   =>  $_POST['IntroductionContent'],
        );
        
        $CI->PA->insert($data);
        echo 1;

    }
    
    public function remove_apply(){
        $UserInfo = $this->session->userdata('UserInfo');
        if($UserInfo['UserType'] != 'work') exit;
        $CI =& get_instance();
        $CI->load->model('Post_Applies_model','PA');
        $CI->load->model('Users2_Notification_model','US2Noti');
        $CI->load->model('Users3_model','Us3');
        $CI->load->model('Posts_model','Posts');
        //Ready Data   
        $ID = $_POST['ID'];
        
        $Post = $CI->Posts->Get(array('PostID'    =>  $ID)); 
        $Post = $Post[0];
        
        $PA = $CI->PA->Get(array('PostApplyID'  =>  $_POST['PAID']));
        $PA = $PA[0];
        
        $Applicant = $CI->Us3->Get(array('RelUserEmail'  =>  $PA['RelUserEmailGroup3']));
        $Applicant = $Applicant[0];
        
        $data = array(
            'RelUserEmailGroup3'    =>  $UserInfo['UserEmail'],
            'RelPostID'             =>  $ID,
        );
        //End of Ready Data
        $CI->PA->Delete($data);
        

        
        $dataNoti = array(
            'Type'          =>  'App_Rev',
            'RelUserEmail'  =>  $Post['RelUserEmail'],
            'Content'       =>  '<b>'.$UserInfo['UserFullname'].'</b> '
                .'has cancel his applyment in: '
                .'<b>'.$Post['Subject'].'</b>',
            'RelPostID'     =>  $ID,
        );
        $Additional = array($UserInfo['UserGroup3ID']);
        $CI->US2Noti->Insert_Noti($dataNoti,$Additional,$Applicant['AvatarLink']);
        echo 1;
    }
    
    public function add_favorite(){
        $PostID = $_POST['PostID'];
        $UserEmail = $_POST['UserEmail'];
        
        $CI =& get_instance();
        $CI->load->model('Favorite_Posts_model','Favorite');  
        $date = date('Y-m-d H:i:s');
        $CI->Favorite->Insert(array(
            'RelPostID' =>  $PostID,
            'RelUserEmail'  => $UserEmail,
            'AddPostTime'   => $date,
        ));
        
        echo "1";
    }
    
    public function remove_favorite(){
        $PostID = $_POST['PostID'];
        $UserEmail = $_POST['UserEmail'];
        
        $CI =& get_instance();
        $CI->load->model('Favorite_Posts_model','Favorite');  
        $date = date('Y-m-d H:i:s');
        $CI->Favorite->Delete(array(
            'RelPostID' =>  $PostID,
            'RelUserEmail'  => $UserEmail,
        ));
        
        echo "1";
    }
}
